﻿//-----------------------------------------------------------------------
// <copyright file="ExceptionProviderUnitTest.cs" company="CodePlex">
//     Copyright (c) Doug Holland, CodePlex. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------
namespace CodePlex.Diagnostics
{
    using System;

    using System.Security;
    using System.Security.Principal;
    
    using System.ServiceModel;

    using CodePlex.Diagnostics.Services;
    
    using Microsoft.VisualStudio.TestTools.UnitTesting;

    /// <summary>
    /// Provides unit tests for the obsolete <see cref="T:CodePlex.Diagnostics.ExceptionProvider"/> class.
    /// </summary>
    [TestClass]
    public class ExceptionProviderUnitTest
    {
        #region public ExceptionProviderUnitTest()

        /// <summary>
        /// Initializes a new instance of the <see cref="T:ExceptionProviderUnitTest"/> class.
        /// </summary>
        public ExceptionProviderUnitTest()
        {
        }
        
        #endregion

        #region public TestContext TestContext

        /// <summary>
        /// Gets or sets the test context which provides information about and functionality for the current test run.
        /// </summary>
        public TestContext TestContext
        {
            get;
            set;
        }

        #endregion

        #region public void ExceptionProviderPublishException()

        /// <summary>
        /// Publish the exception and ensures that the appropriate publication Id is returned from the service.
        /// </summary>
        [TestMethod]
        public void ExceptionProviderPublishException()
        {
            try
            {
                try
                {
                    for (int index = 0; index < 10; index++)
                    {
                        index /= index;
                    }
                }
                catch (DivideByZeroException ex)
                {
                    IIdentity identity = WindowsIdentity.GetCurrent() as IIdentity;

                    // disable warning for the use of obsolete code.
                    #pragma warning disable 618
                    
                    Guid publicationId = ExceptionProvider.Publish(ex, identity);

                    // restore warning for the use of obsolete code.
                    #pragma warning restore 618

                    Assert.AreNotEqual<Guid>(publicationId, Guid.Empty, "The publication id should not be equal to Guid.Empty.");

                    throw new PublishedException(publicationId, ex);
                }
            }
            catch (PublishedException ex)
            {
                Assert.AreNotEqual<Guid>(ex.PublicationId, Guid.Empty, "PublishedException contains the incorrect publication Id.");

                Assert.AreEqual<Guid>(ex.Publish(), ex.PublicationId, "PublishedException contains the incorrect publication id.");
            }
        }

        #endregion
    }
}
